// index.js
const app = getApp();
Page({
    data: {
        pickupActive: true, // 默认选择到店自取
        products: [
            {
                name: '奶皮子手工酸奶(原味)',
                price: '18.8',
                image: 'https://xxx.xjdio.com/static/img/1.jpg'
            },
            {
                name: '奶皮子手工酸奶(莓果)',
                price: '22.8',
                image: 'https://xxx.xjdio.com/static/img/3.jpg'
            },
            {
                name: '奶皮子手工酸奶(抹茶)',
                price: '21.8',
                image: 'https://xxx.xjdio.com/static/img/3.jpg'
            },
            {
                name: '奶皮子手工酸奶(任选)',
                price: '19.9',
                image: 'https://xxx.xjdio.com/static/img/5.jpg'
            }
        ]
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    onShow() {
        // app.setCurrentTab(0);
    },
    navigateTo(e) {
        const url = e.currentTarget.dataset.url;
        wx.navigateTo({
            url: url
        });
    },
    navigateTo(e) {
        const url = e.currentTarget.dataset.url;
        wx.navigateTo({
          url: url
        });
      },
    // 正确的getTabBar函数实现
    getTabBar: function () {
        // 避免递归调用，直接使用selectComponent
        const pages = getCurrentPages();
        if (pages.length === 0) return null;

        const currentPage = pages[pages.length - 1];
        return currentPage.selectComponent('#custom-tab-bar');
    },
    onLoad(option) {
        // console.log("进入" + option)
        // debugger
        // 页面加载
    },
    // 切换取货方式
    switchOption: function (e) {
        const type = e.currentTarget.dataset.type;
        this.setData({
            pickupActive: type === 'pickup'
        });
        wx.switchTab({
          url: '/pages/cart/cart',
        })
    },

    // 打电话
    makePhoneCall: function () {
        wx.makePhoneCall({
            phoneNumber: '12345678901', // 替换为实际电话号码
        })
    },

    // 导航
    openLocation: function () {
        wx.openLocation({
            latitude: 31.338399, // 替换为实际纬度
            longitude: 120.595502, // 替换为实际经度
            scale: 18,
            name: '小鲜谷奶皮子手工酸奶(长虹店)',
            address: '苏州市姑苏区江星路666号'
        })
    },

    // 添加到购物车
    addToCart: function (e) {
        const index = e.currentTarget.dataset.index;
        const product = this.data.products[index];
        wx.showToast({
            title: `已添加${product.name}`,
            icon: 'success'
        })
    },
    navigateTo(e) {
        const url = e.currentTarget.dataset.url;
        wx.navigateTo({
            url: url
        });
    }

})
